package com.java.system.tool;

import oshi.hardware.*;
import oshi.software.os.*;
import oshi.util.FormatUtil;

import java.util.Arrays;
import java.util.List;


public class SystemInfo {

    public static void main(String[] args) {

        HardwareAbstractionLayer hardware = OshiTools.getHardware();

        ComputerSystem computerSystem = hardware.getComputerSystem();
        printComputerSystem(computerSystem);

        OperatingSystem os = OshiTools.getOs();
        printOperatingSystem(os);

        FileSystem fileSystem = os.getFileSystem();
        printFileSystem(fileSystem);

        CentralProcessor processor = hardware.getProcessor();
        printProcessor(processor);

        GlobalMemory memory = hardware.getMemory();
        printMemory(memory);

        List<OSProcess> processes = os.getProcesses(5, OperatingSystem.ProcessSort.CPU);
        printOSProcess(processes,memory);

        List<HWDiskStore> diskStores = hardware.getDiskStores();
        printDiskStores(diskStores);

        List<NetworkIF> networkIFs = hardware.getNetworkIFs();
        printNetworkInterfaces(networkIFs);

        NetworkParams networkParams = os.getNetworkParams();
        printNetworkParameters(networkParams);

        List<UsbDevice> usbDevices = hardware.getUsbDevices(true);
        printUsbDevices(usbDevices);

        Sensors sensors = hardware.getSensors();
        printSensors(sensors);

        List<PowerSource> powerSources = hardware.getPowerSources();
        printPowerSources(powerSources);

        List<Display> displays = hardware.getDisplays();
        printDisplay(displays);
    }




    public static void printComputerSystem(ComputerSystem computerSystem) {
        Firmware firmware = computerSystem.getFirmware();
        Baseboard baseboard = computerSystem.getBaseboard();
        printTools.printAll("硬件环境", Arrays.asList("硬件制造商", "硬件型号", "硬件编码"), Arrays.asList(computerSystem.getManufacturer(), computerSystem.getModel(), computerSystem.getSerialNumber()));
        printTools.printAll("固件信息", Arrays.asList("固件制造商", "固件名称", "固件版本", "固件说明", "固件日期"), Arrays.asList(firmware.getManufacturer(), firmware.getName(), firmware.getVersion(), firmware.getDescription(), firmware.getReleaseDate()));
        printTools.printAll("主板信息", Arrays.asList("主板制造商", "主板型号", "主板版本", "主板编码"), Arrays.asList(baseboard.getManufacturer(), baseboard.getModel(), baseboard.getVersion(), baseboard.getSerialNumber()));
    }

    public static void printOperatingSystem(OperatingSystem os) {
        printTools.printAll("操作系统", Arrays.asList("操作系统制造商", "操作系统", "操作系统进程数", "操作系统线程数", "操作系统开始时间", "操作系统位数", "操作系统结束时间"), Arrays.asList(os.getManufacturer(), String.valueOf(os.getFamily()), String.valueOf(os.getProcessCount()), String.valueOf(os.getThreadCount()), String.valueOf(os.getSystemBootTime()), String.valueOf(os.getBitness()), String.valueOf(os.getSystemUptime())));
    }

    public static void printProcessor(CentralProcessor centralProcessor) {
        List<String> keys = Arrays.asList("cpu数量", "cpu核心数", "cpu线程数", "cpu编号");
        List<String> values = Arrays.asList(String.valueOf(centralProcessor.getPhysicalPackageCount()), String.valueOf(centralProcessor.getPhysicalProcessorCount()), String.valueOf(centralProcessor.getLogicalProcessorCount()), String.valueOf(centralProcessor.getProcessorIdentifier()));
        printTools.printAll("中央处理器", keys, values);
    }

    public static void printMemory(GlobalMemory memory) {
        List<String> keys = Arrays.asList("内存大小", "可用内存大小");
        List<String> values = Arrays.asList(FormatUtil.formatBytes(memory.getTotal()), FormatUtil.formatBytes(memory.getAvailable()));
        printTools.printAll("内存", keys, values);
        List<PhysicalMemory> memoryList = memory.getPhysicalMemory();
        memoryList.forEach((item) -> {
            List<String> keyList = Arrays.asList("内存型号", "内存规格", "内存主频", "内存大小");
            List<String> valueList = Arrays.asList(item.getManufacturer(), item.getMemoryType(), FormatUtil.formatBytes(item.getClockSpeed()), FormatUtil.formatBytes(item.getCapacity()));
            printTools.printAll("内存条信息", keyList, valueList);
        });
    }

    public static void printUsbDevices(List<UsbDevice> usbDeviceList) {
        usbDeviceList.forEach((item) -> {
//            List<String> keyList = Arrays.asList("Usb驱动名称", "Usb产品ID", "Usb编码", "Usb唯一设备号", "Usb供应商", "UsbUsb供应商ID");
//            List<String> valueList = Arrays.asList(item.getName(), item.getProductId(), item.getSerialNumber(), item.getUniqueDeviceId(), item.getVendor(), item.getVendorId());
            List<String> keyList = Arrays.asList("Usb驱动");
            List<String> valueList = Arrays.asList(item.toString());
            printTools.printAll("Usb驱动信息", keyList, valueList);
        });
    }

    private static void printFileSystem(FileSystem fileSystem) {
        List<OSFileStore> fileStores = fileSystem.getFileStores();
        fileStores.forEach((item) -> {
            List<String> keyList = Arrays.asList("名称", "UUID", "盘符", "文件类型", "总大小", "剩余大小", "使用比例%");
            List<String> valueList = Arrays.asList(item.getName(), item.getUUID(), item.getMount(), item.getType(), FormatUtil.formatBytes(item.getTotalSpace()), FormatUtil.formatBytes(item.getUsableSpace()), String.valueOf(100d * item.getUsableSpace() / item.getTotalSpace()));
            printTools.printAll("文件存储", keyList, valueList);
        });
    }

    private static void printDiskStores(List<HWDiskStore> diskList) {
        diskList.forEach((item) -> {
            List<String> keyList = Arrays.asList("名称", "型号", "序号", "大小");
            List<String> valueList = Arrays.asList(item.getName(), item.getModel(), item.getSerial(), FormatUtil.formatBytes(item.getSize()));
            printTools.printAll("磁盘存储", keyList, valueList);
            List<HWPartition> partitions = item.getPartitions();
            partitions.forEach(e->{
                List<String> keyLists = Arrays.asList("分区名称", "分区类型", "分区大小");
                List<String> valueLists = Arrays.asList(e.getName(), e.getType(),FormatUtil.formatBytesDecimal(e.getSize()));
                printTools.printAll("磁盘分区", keyLists, valueLists);
            });
        });
    }

    private static void printNetworkInterfaces(List<NetworkIF> networkIFs) {
        networkIFs.forEach((item) -> {
            List<String> keyList = Arrays.asList("名称", "MAC", "MTU", "网速bps", "IPv4", "IPv6");
            List<String> valueList = Arrays.asList(item.getName(), item.getMacaddr(),
                    String.valueOf(item.getMTU()), FormatUtil.formatValue(item.getSpeed(), "bps"), Arrays.toString(item.getIPv4addr()), Arrays.toString(item.getIPv6addr()));
            printTools.printAll("网络设置", keyList, valueList);
        });
    }

    private static void printNetworkParameters(NetworkParams networkParams) {
        List<String> keyList = Arrays.asList("Host", "Domain", "DNS", "IPv4", "IPv6");
        List<String> valueList = Arrays.asList(networkParams.getHostName(), networkParams.getDomainName(),
                Arrays.toString(networkParams.getDnsServers()), networkParams.getIpv4DefaultGateway()
                , networkParams.getIpv6DefaultGateway());
        printTools.printAll("网络", keyList, valueList);
    }

    private static void printSensors(Sensors sensors) {
        List<String> keyList = Arrays.asList("CPU温度", "风扇", "CPU电压");
        List<String> valueList = Arrays.asList(String.valueOf(sensors.getCpuTemperature()),
                Arrays.toString(sensors.getFanSpeeds()), String.valueOf(sensors.getCpuVoltage()));
        printTools.printAll("传感器", keyList, valueList);
    }

    private static void printPowerSources(List<PowerSource> powerSources) {
        powerSources.stream().forEach(item -> {
            List<String> keyList = Arrays.asList("电源名称", "剩余百分比");
            List<String> valueList = Arrays.asList(item.getName(), String.valueOf(item.getRemainingCapacityPercent()*100));
            printTools.printAll("电源", keyList, valueList);
        });
    }

    private static void printDisplay(List<Display> displays) {
        displays.stream().forEach(item -> {
            List<String> keyList = Arrays.asList("显示");
            List<String> valueList = Arrays.asList(item.toString());
            printTools.printAll("显示", keyList, valueList);
        });
    }


    private static void printOSProcess(List<OSProcess> processes,GlobalMemory memory) {
        processes.stream().forEach(item -> {
            List<String> keyList = Arrays.asList("PID","CPU","MEM","VSZ","RSS Name");
            List<String> valueList = Arrays.asList(String.valueOf(item.getProcessID()),String.valueOf(100d * (item.getKernelTime() + item.getUserTime()) / item.getUpTime()),
            String.valueOf(100d * item.getResidentSetSize() / memory.getTotal()),FormatUtil.formatBytes(item.getVirtualSize()),
                            FormatUtil.formatBytes(item.getResidentSetSize()),item.getName());
            printTools.printAll("进程ID", keyList, valueList);
        });
    }
}
